package pcb.views 
{
	import com.intel.utils.TextRender;
	import com.gskinner.motion.GTweeny;
	import com.intel.utils.Debugger;
	import fl.motion.easing.*;
	import com.intel.core.UIComponent;
	import flash.events.MouseEvent;
	import pcb.models.LocaleModel;
	import pcb.views.uievents.UIEvent;
	import flash.display.MovieClip;
	import flash.events.Event;

	/**
	 * ...
	 * @author Fintan Boyle
	 */
	public class LessonMenu extends UIComponent implements ILocalise 
	{
		
		private var menu_tree:MovieClip;
		private var nextLessonBut:MovieClip;
		private var prevLessonBut:MovieClip;
		
		private var prevButTxt:String = "Previous Lesson";
		private var nextButTxt:String = "Next Lesson";
		
		public function LessonMenu() 
		{
		}
		
		public function localiseLabels(localeModel:LocaleModel):void 
		{
			prevButTxt = localeModel.getLabelbyId("prevLesson_button");
			nextButTxt = localeModel.getLabelbyId("nextLesson_button");
		}
		
		public function createMenu(menu:MovieClip, dataprovider:XML):void 
		{
			menu_tree = menu;
			menu_tree.addDataProvider(dataprovider);
			menu_tree.addEventListener(UIEvent.MENU_ITEM_CLICKED, onMenuItemClicked);
			menu_tree.addEventListener(UIEvent.MENU_NO_ITEM_CLICKED, onNoMenuItemClicked);
			menu_tree.alpha = 0;
			addChild(menu_tree);
			var gTween:GTweeny = new GTweeny(menu_tree, 1.5, null, { ease:Sine.easeInOut } );
			gTween.setProperty("alpha", 1);
			
			nextLessonBut = this.getAppRoot().getChildByName("nextLessonBut") as MovieClip;
			prevLessonBut = this.getAppRoot().getChildByName("prevLessonBut") as MovieClip;
			
			if (nextLessonBut) {
				//nextLessonBut.useHandCursor = true;
				nextLessonBut.addEventListener(MouseEvent.CLICK, nextLessonBut_onClick,false,0,true);
				nextLessonBut.addEventListener(MouseEvent.ROLL_OVER, nextLessonBut_onRollover,false,0,true);
				nextLessonBut.addEventListener(MouseEvent.ROLL_OUT, nextLessonBut_onRollout,false,0,true);
			}
			if (prevLessonBut) {
				//prevLessonBut.useHandCursor = true;
				prevLessonBut.addEventListener(MouseEvent.CLICK, prevLessonBut_onClick,false,0,true);
				prevLessonBut.addEventListener(MouseEvent.ROLL_OVER, prevLessonBut_onRollover,false,0,true);
				prevLessonBut.addEventListener(MouseEvent.ROLL_OUT, prevLessonBut_onRollout,false,0,true);
			}
			
		}
		
		public function set enableMenu(boo:Boolean):void 
		{
			menu_tree.isEnabled = boo;
			if (boo) {				
				nextLessonBut.useHandCursor = true;
				nextLessonBut.addEventListener(MouseEvent.CLICK, nextLessonBut_onClick,false,0,true);
				nextLessonBut.addEventListener(MouseEvent.ROLL_OVER, nextLessonBut_onRollover,false,0,true);
				nextLessonBut.addEventListener(MouseEvent.ROLL_OUT, nextLessonBut_onRollout,false,0,true);
				prevLessonBut.useHandCursor = true;
				prevLessonBut.addEventListener(MouseEvent.CLICK, prevLessonBut_onClick,false,0,true);
				prevLessonBut.addEventListener(MouseEvent.ROLL_OVER, prevLessonBut_onRollover,false,0,true);
				prevLessonBut.addEventListener(MouseEvent.ROLL_OUT, prevLessonBut_onRollout,false,0,true);			
			} else {
				nextLessonBut.useHandCursor = false;
				nextLessonBut.removeEventListener(MouseEvent.CLICK, nextLessonBut_onClick);
				nextLessonBut.removeEventListener(MouseEvent.ROLL_OVER, nextLessonBut_onRollover);
				nextLessonBut.removeEventListener(MouseEvent.ROLL_OUT, nextLessonBut_onRollout);
				prevLessonBut.useHandCursor = false;
				prevLessonBut.removeEventListener(MouseEvent.CLICK, prevLessonBut_onClick);
				prevLessonBut.removeEventListener(MouseEvent.ROLL_OVER, prevLessonBut_onRollover);
				prevLessonBut.removeEventListener(MouseEvent.ROLL_OUT, prevLessonBut_onRollout);
			}
		}
		
		private function nextLessonBut_onClick(e:MouseEvent):void 
		{
			/*
			Debugger.traceOut("nextLessonBut_onClick");
			var evNxt:UIEvent = new UIEvent(UIEvent.CLICKED);
			evNxt.id = "next-lesson";
			dispatchEvent(evNxt);
			*/
			menu_tree.next();			
		}
		
		private function prevLessonBut_onClick(e:MouseEvent):void 
		{
			/*
			Debugger.traceOut("prevLessonBut_onClick");
			var evPrev:UIEvent = new UIEvent(UIEvent.CLICKED);
			evPrev.id = "previous-lesson";
			dispatchEvent(evPrev);
			*/
			menu_tree.previous();
		}
		
		private function onMenuItemClicked(e:UIEvent):void 
		{
			this.enableMenu = false;
			enableNextButton(false);
			enablePrevButton(false);
			dispatchEvent(e);
		}
		
		private function onNoMenuItemClicked(e:UIEvent):void 
		{
			dispatchEvent(e);
		}
		
		private function prevLessonBut_onRollout(e:MouseEvent):void 
		{
			var mc:MovieClip = e.target as MovieClip;
			mc.gotoAndStop("up");
		}
		
		private function prevLessonBut_onRollover(e:MouseEvent):void 
		{
			var mc:MovieClip = e.target as MovieClip;
			mc.gotoAndStop("over");
			TextRender.createLabel(mc.mcLabel.txt_label, prevButTxt); 			
		}
		
		private function nextLessonBut_onRollout(e:MouseEvent):void 
		{
			var mc:MovieClip = e.target as MovieClip;
			mc.gotoAndStop("up");
		}
		
		private function nextLessonBut_onRollover(e:MouseEvent):void 
		{
			var mc:MovieClip = e.target as MovieClip;
			mc.gotoAndStop("over");
			TextRender.createLabel(mc.mcLabel.txt_label, nextButTxt);			
		}
		
		public function enableNextButton(boo:Boolean):void 
		{
			nextLessonBut.mouseEnabled = boo;
			//nextLessonBut.visible = boo;
		}
		
		public function enablePrevButton(boo:Boolean):void 
		{
			prevLessonBut.mouseEnabled = boo;
			//prevLessonBut.visible = boo;
		}
		
		/*
		 * 
		 * public function selectNode(id:String):void 
		{
			if (menu_tree) {
				menu_tree.selectNode(id);
			}
		}
		
		public function destroy():void 
		{
			if (menu_tree.hasEventListener(UIEvent.MENU_ITEM_CLICKED)) {
				menu_tree.removeEventListener(UIEvent.MENU_ITEM_CLICKED, onMenuItemClicked);
			}
			if (menu_tree.hasEventListener(UIEvent.MENU_NO_ITEM_CLICKED)) {
				menu_tree.removeEventListener(UIEvent.MENU_NO_ITEM_CLICKED, onNoMenuItemClicked);
			}
			menu_tree.destroy();
			menu_tree = null;
			
		}
		*/
	}
	
}